"""
Скрипт для загрузки приекта из репозитория.

Author: Milinuri Nirvalen
Ver: 1.0.2 (4)
"""

import aiohttp
import asyncio
import os
import zipfile


url = "https://notabug.org/milinuri/ChioHub/archive/master.zip"
repo_name = "chiohub"


def log(text):
	print(f'\033[93m[*] \033[92m{text}\033[0m')


async def main(url):
	async with aiohttp.ClientSession() as session:
		async with session.get(url) as resp:
			with open('chio.zip', 'wb') as f:
				f.write(await resp.read())

	if zipfile.is_zipfile('chio.zip'):
		log('архив проверен.')
		with zipfile.ZipFile('chio.zip', 'r') as z:
			z.extractall()
		log('архив распакован.')
		os.remove('chio.zip')

		for x in os.listdir(repo_name):
			os.replace(f"{repo_name}/{x}", x)
		os.rmdir(repo_name)
		log('файлы перемещены.')
	else:
		exit('[!] ошибка при скачивании.')

if __name__ == '__main__':
	try:
		asyncio.run(main(url))

	except Exception as e:
		exit(f'[!] Упс, {e}.')

	else:
		log('архив с репозиторием получен.')
